package com.ly.emp.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.entity.HubTeamAdd;
import com.ly.emp.entity.HubTeamAddType;
import com.ly.emp.mapper.HubTeamAddMapper;
import com.ly.emp.mapper.HubTeamAddTypeMapper;
import com.ly.emp.service.IHubTeamAddTypeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 一线班组-加减分类型维护-impl
 *
 * @author luoyun
 * @Date: 2025-06-20
 */
@Service
@RequiredArgsConstructor
public class HubTeamAddTypeServiceImpl extends ServiceImpl<HubTeamAddTypeMapper, HubTeamAddType> implements IHubTeamAddTypeService {

    private final HubTeamAddMapper addMapper;

    @Override
    public R getId(String id) {
        HubTeamAddType entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamAddType entity) {
        List<HubTeamAddType> list = this.list(
                new LambdaQueryWrapper<HubTeamAddType>()
                        .eq(StringUtils.isNotBlank(entity.getOrgCode()), HubTeamAddType::getOrgCode, entity.getOrgCode())
                        .eq(StrUtil.isNotBlank(entity.getIndexTypeCode()), HubTeamAddType::getIndexTypeCode, entity.getIndexTypeCode())
                        .like(StrUtil.isNotBlank(entity.getIndexTypeName()), HubTeamAddType::getIndexTypeName, entity.getIndexTypeName())
                        .orderByAsc(HubTeamAddType::getDisplayOrder)
                        .orderByDesc(HubTeamAddType::getSysUpdateDate)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamAddType entity, Page<HubTeamAddType> page) {
        IPage<HubTeamAddType> pageList = this.page(
                page,
                new LambdaQueryWrapper<HubTeamAddType>()
                        .eq(StringUtils.isNotBlank(entity.getOrgCode()), HubTeamAddType::getOrgCode, entity.getOrgCode())
                        .eq(StrUtil.isNotBlank(entity.getIndexTypeCode()), HubTeamAddType::getIndexTypeCode, entity.getIndexTypeCode())
                        .like(StrUtil.isNotBlank(entity.getIndexTypeName()), HubTeamAddType::getIndexTypeName, entity.getIndexTypeName())
                        .orderByAsc(HubTeamAddType::getDisplayOrder)
                        .orderByDesc(HubTeamAddType::getSysUpdateDate)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamAddType entity) {
        String orgCode = entity.getOrgCode();
        String indexTypeName = entity.getIndexTypeName();
        HubTeamAddType teamAddType = this.getOne(new LambdaQueryWrapper<HubTeamAddType>()
                .eq(HubTeamAddType::getOrgCode, orgCode)
                .eq(HubTeamAddType::getIndexTypeName, indexTypeName));
        if (ObjUtil.isNotEmpty(teamAddType)) {
            return R.fail("加减分类型名称已存在！");
        }
        entity.setIndexTypeCode(IdUtil.getSnowflakeNextIdStr());
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamAddType entity) {
        List<HubTeamAddType> list = this.list();
        AtomicBoolean result = new AtomicBoolean(false);
        list.stream().filter(n -> !entity.getId().equals(n.getId())).forEach(n -> {
            if ((entity.getIndexTypeName().equals(n.getIndexTypeName())) && (entity.getOrgCode().equals(n.getOrgCode()))) {
                result.set(true);
            }
        });
        if (result.get()) {
            return R.fail("加减分类型名称已存在！");
        }
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        HubTeamAddType hubTeamAddType = this.getById(id);
        addMapper.delete(
                new LambdaQueryWrapper<HubTeamAdd>()
                        .eq(HubTeamAdd::getIndexTypeCode, hubTeamAddType.getIndexTypeCode())
        );
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamAddType> dto) {
        List<HubTeamAddType> datas = dto.getDatas();
        datas.stream().filter(n -> StrUtil.isBlank(n.getIndexTypeCode())).forEach(n -> {
            n.setIndexTypeCode(IdUtil.getSnowflakeNextIdStr());
        });
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public String getAndAdd(HubTeamAddType entity) {
        HubTeamAddType one = this.getOne(
                new LambdaQueryWrapper<HubTeamAddType>()
                        .eq(HubTeamAddType::getOrgCode, entity.getOrgCode())
                        .eq(HubTeamAddType::getIndexTypeName, entity.getIndexTypeName())
        );
        if(one != null){
            return one.getIndexTypeCode();
        }
        // //设置默认父编码
        // if (StrUtil.isBlank(entity.getParentTypeCode())) {
        //     entity.setParentTypeCode("0");
        // }
        String uidCode = IdUtil.getSnowflakeNextIdStr();
        entity.setIndexTypeCode(uidCode);
        this.save(entity);
        return uidCode;
    }
}
